﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using System.Security.Principal;

using log4net;

namespace RuiJing.Web
{
    // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明，
    // 请访问 http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        private static ILog Log = Code.Commen.Log.GetLog(typeof(MvcApplication));

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            var urlext = Global.UrlRewrite ? Global.UrlExt : string.Empty;


            routes.MapRoute(
                "lg_4",                                                 // 路由名称
                "{l}/{controller}/{action}/{a}/{b}/{c}/{d}" + urlext,   // 带有参数的 URL
                new { controller = "home", action = "index" },          // 参数默认值
                new { l = @"En|Ch|en|ch" },
                new string[] { "RuiJing.Web.Controllers" }
            );

            routes.MapRoute(
                "lg_3",
                "{l}/{controller}/{action}/{a}/{b}/{c}" + urlext,
                new { controller = "home", action = "index" },
                new { l = @"En|Ch|en|ch" },
                new string[] { "RuiJing.Web.Controllers" }
            );

            routes.MapRoute(
                "lg_2",
                "{l}/{controller}/{action}/{a}/{b}" + urlext,
                new { controller = "home", action = "index" },
                new { l = @"En|Ch|en|ch" },
                new string[] { "RuiJing.Web.Controllers" }
            );
            routes.MapRoute(
                "lg_1", // 路由名称
                "{l}/{controller}/{action}/{a}" + urlext,
                new { controller = "home", action = "index" },
                new { l = @"En|Ch|en|ch" },
                new string[] { "RuiJing.Web.Controllers" }
            );
            routes.MapRoute(
                "lg_l",
                "{l}" + urlext,
                new { controller = "home", action = "index" } ,
                new { l = @"En|Ch|en|ch" },
                new string[] { "RuiJing.Web.Controllers" }
            );
            routes.MapRoute(
                "lg_c",
                "{l}/{controller}" + urlext,
                new { controller = "home", action = "index" },
                new { l = @"En|Ch|en|ch" },
                new string[] { "RuiJing.Web.Controllers" }
            );
            routes.MapRoute(
                "lg_a",
                "{l}/{controller}/{action}" + urlext, 
                new { controller = "home", action = "index" },
                new { l = @"En|Ch|en|ch" },
                new string[] { "RuiJing.Web.Controllers" }
            );

            routes.MapRoute(
                "d_a",
                "{controller}" + urlext,
                new { controller = "home", action = "index" },
                new string[] { "RuiJing.Web.Controllers" }
                );

            routes.MapRoute(
                "d_h",
                "{controller}/{action}" + urlext,
                new { controller = "home", action = "index" },
                new string[] { "RuiJing.Web.Controllers" }
                );

            routes.MapRoute(
                "Default",
                "{controller}/{action}/{a}",
                new { controller = "home", action = "index", a = UrlParameter.Optional },
                new string[] { "RuiJing.Web.Controllers" }
            );
        }
        
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterRoutes(RouteTable.Routes);

            Log.Info("启动服务");
            //RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
        }

        protected void FormsAuthentication_Authenticate(object sender, FormsAuthenticationEventArgs e)
        {
            if (FormsAuthentication.CookiesSupported)
            {
                HttpCookie authtick = Request.Cookies[FormsAuthentication.FormsCookieName];
                if (authtick == null)
                    return;

                FormsAuthenticationTicket tick = FormsAuthentication.Decrypt(authtick.Value);

                if (tick == null || tick.Expired)
                    return;
                var aus = System.Configuration.ConfigurationManager.GetSection("system.web/authentication") as System.Web.Configuration.AuthenticationSection;
                if (aus != null && aus.Forms.SlidingExpiration && (tick.Expiration - DateTime.Now).TotalMinutes > aus.Forms.Timeout.TotalMinutes / 2)
                {
                    tick = new FormsAuthenticationTicket(2, tick.Name, tick.IssueDate, DateTime.Now.AddMinutes(aus.Forms.Timeout.TotalMinutes), false, tick.UserData);
                    HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(tick));
                    Response.Cookies.Remove(FormsAuthentication.FormsCookieName);
                    Response.Cookies.Add(cookie);
                }

                Context.User = new GenericPrincipal(new GenericIdentity(tick.Name), (tick.UserData ?? string.Empty).Split(','));
            }

        }
        protected void Session_End(object sender, EventArgs e)
        {
            Guid? manage = Session["AdminID"] as Guid?;
            if (manage.HasValue)
            {
                lock (Global.InlineAdmin)
                {
                    Global.InlineAdmin.Remove(manage.Value);
                }
            }

        }
    }

}